/*
 *
 * -------------------------------------------------------------------------------------------------
 *                            COPYRIGHT(C) BOCO HEALTH 2016
 * -------------------------------------------------------------------------------------------------
 *
 * =================================================================================================
 *  *HISTORY
 *
 *      Tag                Date            Author           Description
 * =================================================================================================
 *      created            2017/1/17      caodailu        create
 * =================================================================================================
 *
 *
 */

package com.boco.familyuser.custom;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.boco.familyuser.R;


public class CustomDialog extends Dialog {


    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Context mContext;//上下文
        private String title;//对话框标题
        private int titleSize = -1;//对话框标题字体大小
        private int titleColor = -1;//对话框标题字体颜色
        private String message;//对话框内容
        private int messageSize = -1;//对话框内容字体大小
        private int messageColor = -1;//对话框内容字体颜色
        private String confirm_btnText;//按钮名称"确定"
        private String cancel_btnText;//按钮名称"取消"
        private String neutral_btnText;//按钮名称"隐藏"
        private View contentView;//对话框中间加载的布局
        /*按钮监听事件*/
        private OnClickListener confirm_btnClickListener;
        private OnClickListener cancel_btnClickListener;
        private OnClickListener neutral_btnClickListener;
        private OnEditedListener mOnEditedListener;
        private boolean isEdit =false;

        /**
         * 含有edittext的弹出框监听
         */
        public interface OnEditedListener{
            public void OnEdited(String editStr);
        }
        public Builder(Context context) {
            mContext = context;
        }

        /**
         * 设置对话框消息
         *
         * @param message
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * 设置对话框内容字体大小
         *
         * @param messageSize
         * @return
         */
        public Builder setMessageSize(int messageSize) {
            this.messageSize = messageSize;
            return this;
        }

        /**
         * 设置对话框内容字体颜色
         *
         * @param messageColor
         * @return
         */
        public Builder setMessageColor(int messageColor) {
            this.messageColor = messageColor;
            return this;
        }

        /**
         * 设置对话框消息
         *
         * @param message
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) mContext.getText(message);
            return this;
        }

        /**
         * 设置对话框标题
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) mContext.getText(title);
            return this;
        }

        /**
         * 设置对话框标题字体大小
         *
         * @param titleSize
         * @return
         */
        public Builder setTitleSize(int titleSize) {
            this.titleSize = titleSize;
            return this;
        }

        /**
         * 设置对话框标题
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * 设置对话框标题颜色
         *
         * @param titleColor
         * @return
         */
        public Builder setTitleColor(int titleColor) {
            this.titleColor = titleColor;
            return this;
        }

        /**
         * 设置对话框界面
         *
         * @param view
         * @return
         */
        public Builder setContentView(View view) {
            this.contentView = view;
            return this;
        }

        public Builder setEdit(boolean edit) {
            isEdit = edit;
            return this;
        }

        public Builder setOnEditedListener(OnEditedListener onEditedListener) {
            mOnEditedListener = onEditedListener;
            return this;
        }
        /**
         * 设置确定按钮文字以及监听事件
         *
         * @param confirm_btnText
         * @param listener
         * @return
         */
        public Builder setPositiveButton(int confirm_btnText, OnClickListener listener) {
            this.confirm_btnText = (String) mContext.getText(confirm_btnText);
            this.confirm_btnClickListener = listener;
            return this;
        }

        /**
         * 设置确定按钮文字以及监听事件
         *
         * @param confirm_btnText
         * @param listener
         * @return
         */
        public Builder setPositiveButton(String confirm_btnText, OnClickListener listener) {
            this.confirm_btnText = confirm_btnText;
            this.confirm_btnClickListener = listener;
            return this;
        }

        /**
         * 设置取消按钮资源以及监听事件
         *
         * @param cancel_btnText
         * @return
         */
        public Builder setNegativeButton(int cancel_btnText,
                                         OnClickListener listener) {
            this.cancel_btnText = (String) mContext.getText(cancel_btnText);
            this.cancel_btnClickListener = listener;
            return this;
        }

        /**
         * 设置取消按钮资源以及监听事件
         *
         * @param cancel_btnText
         * @return
         */
        public Builder setNegativeButton(String cancel_btnText,
                                         OnClickListener listener) {
            this.cancel_btnText = cancel_btnText;
            this.cancel_btnClickListener = listener;
            return this;
        }

        /**
         * 设置隐藏按钮资源以及监听事件
         *
         * @param neutral_btnText
         * @return
         */
        public Builder setNeutralButton(int neutral_btnText,
                                        OnClickListener listener) {
            this.neutral_btnText = (String) mContext.getText(neutral_btnText);
            this.neutral_btnClickListener = listener;
            return this;
        }

        /**
         * 设置隐藏按钮资源以及监听事件
         *
         * @param neutral_btnText
         * @return
         */
        public Builder setNeutralButton(String neutral_btnText,
                                        OnClickListener listener) {
            this.neutral_btnText = neutral_btnText;
            this.neutral_btnClickListener = listener;
            return this;
        }

        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context
                    .LAYOUT_INFLATER_SERVICE);
            final CustomDialog dialog = new CustomDialog(mContext, R.style.CustomDialog);
            View layout = inflater.inflate(R.layout.layout_dialog, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams
                    .MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            final EditText editText = (EditText) layout.findViewById(R.id.edit);
            if(isEdit){
                editText.setVisibility(View.VISIBLE);
                ((TextView) layout.findViewById(R.id.message)).setVisibility(View.GONE);

            }

            ((TextView) layout.findViewById(R.id.title)).setText(title);
            ((TextView) layout.findViewById(R.id.title)).getPaint().setFakeBoldText(true);
            if (titleColor != -1) {
                ((TextView) layout.findViewById(R.id.title)).setTextColor(ContextCompat.getColor
                        (mContext, titleColor));
            }
            if (titleSize != -1) {
                ((TextView) layout.findViewById(R.id.title)).setTextSize(titleSize);
            }
            if (title == null || title.trim().equals("")) {
                ((TextView) layout.findViewById(R.id.message)).setGravity(Gravity.CENTER);
            }

            if (neutral_btnText != null && confirm_btnText != null && cancel_btnText != null) {
                ((Button) layout.findViewById(R.id.confirm_btn)).setText(confirm_btnText);
                if (neutral_btnClickListener != null) {
                    ((Button) layout.findViewById(R.id.neutral_btn)).setOnClickListener(new View
                            .OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            neutral_btnClickListener.onClick(dialog, BUTTON_NEUTRAL);
                            dialog.dismiss();
                        }
                    });
                } else {
                    ((Button) layout.findViewById(R.id.neutral_btn)).setOnClickListener(new View
                            .OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                }
            } else {
                layout.findViewById(R.id.neutral_btn).setVisibility(View.GONE);
                layout.findViewById(R.id.single_line).setVisibility(View.GONE);
            }

            if (confirm_btnText != null) {
                ((Button) layout.findViewById(R.id.confirm_btn))
                        .setText(confirm_btnText);
                if (confirm_btnClickListener != null) {
                    ((Button) layout.findViewById(R.id.confirm_btn))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    confirm_btnClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                    dialog.dismiss();
                                }
                            });
                } else {
                    ((Button) layout.findViewById(R.id.confirm_btn))
                            .setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            });
                }
            } else {
                layout.findViewById(R.id.confirm_btn).setVisibility(View.GONE);
                layout.findViewById(R.id.second_line).setVisibility(View.GONE);
                layout.findViewById(R.id.cancel_btn).setBackgroundResource(
                        R.drawable.selected_red_button);
            }
            // set the cancel button
            if (cancel_btnText != null) {
                ((Button) layout.findViewById(R.id.cancel_btn))
                        .setText(cancel_btnText);
                if (cancel_btnClickListener != null) {
                    ((Button) layout.findViewById(R.id.cancel_btn))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    cancel_btnClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                    dialog.dismiss();
                                }
                            });
                } else {
                    ((Button) layout.findViewById(R.id.cancel_btn))
                            .setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            });
                }
            } else {
                layout.findViewById(R.id.cancel_btn).setVisibility(View.GONE);
                layout.findViewById(R.id.second_line).setVisibility(View.GONE);
                layout.findViewById(R.id.confirm_btn).setBackgroundResource(
                        R.drawable.selected_green_button);
            }
            if (message != null) {
                ((TextView) layout.findViewById(R.id.message)).setText(message);
                if (messageColor != -1) {
                    ((TextView) layout.findViewById(R.id.message)).setTextColor(ContextCompat
                            .getColor(mContext, messageColor));
                }
                if (messageSize != -1) {
                    ((TextView) layout.findViewById(R.id.message)).setTextSize(messageSize);
                }
            } else if (contentView != null) {
                ((RelativeLayout) layout.findViewById(R.id.message_relative))
                        .removeAllViews();
                ((RelativeLayout) layout.findViewById(R.id.message_relative)).addView(
                        contentView, new ViewGroup.LayoutParams(
                                ViewGroup.LayoutParams.WRAP_CONTENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT));
            }
            dialog.setContentView(layout);
            return dialog;

        }
    }
}
